//
//  Car.swift
//  Automobile
//
//  Created by erkebulan elzhan on 7/28/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import Foundation



struct CarNumber: Decodable {
    var isNewFormat: Bool
    var number: Int
    var letters: String
    var cityId: String
    var vin: String?
    
    var fullNumber: String {
        return isNewFormat ? "\(number)" + letters  + "\(cityId)" : cityId + "\(number)" + letters
    }
}
struct CarColor: Decodable {
    var id: Int
    var name: String
    var ext: Int
    var r_abbreviation: Int?
    var g_abbreviation: Int?
    var b_abbreviation: Int?
}
struct IdAndName: Decodable {
    var id: Int
    var ext: Int?=nil
    var name: String
}
struct HistoryStatus: Decodable {
    var is_already_purchased: Bool = false
    var count: Int = 0
    var order_id: Int = 0
}


struct Car: Decodable {
    
    var id: Int?
    var carName: String?
    var carNumber: CarNumber?
    var color: CarColor?
    var marka: Marka?
    var model: Model?
    var generation: Generation?
    var serie: Serie?
    var modification: Modification?
    var carType: IdAndName?
    
    var bodyType: IdAndName?
    var transmissionType: IdAndName?
    var driveType: IdAndName?
    var engineType: IdAndName?
    
    var engineVolume: Double?
    var enginePower: Double?
    var trunkVolume: Double?
    var roadClearance: Double?
    var acceleration: Double?
    var maxSpeed: Double?
    var maxTorque: Double?
    var fuelConsumption: Double?
    var year: Int?
    var mileage: Int?
    
    
    var dtp_status = HistoryStatus()
    var history_status = HistoryStatus()
    
    
    enum CodingKeys: String, CodingKey {
        case state_number, vin
        case car_model, car_mark, car_generation, car_modification, car_serie
        case id, title, engine_volume, engine_power, year
        case car_color, transmission_type, drive_type, engine_type, body_type
        case dtp_status, history_status, mileage
    }
    
    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        
        marka = try? values.decode(Marka.self, forKey: .car_mark)
        model = try? values.decode(Model.self, forKey: .car_model)
        generation = try? values.decode(Generation.self, forKey: .car_generation)
        serie = try? values.decode(Serie.self, forKey: .car_serie)
        modification = try? values.decode(Modification.self, forKey: .car_modification)
        color = try? values.decode(CarColor.self, forKey: .car_color)
        transmissionType = try? values.decode(IdAndName.self, forKey: .transmission_type)
        driveType = try? values.decode(IdAndName.self, forKey: .drive_type)
        engineType = try? values.decode(IdAndName.self, forKey: .engine_type)
        bodyType = try? values.decode(IdAndName.self, forKey: .body_type)
        engineVolume = try? values.decode(Double.self, forKey: .engine_volume)
        enginePower = try? values.decode(Double.self, forKey: .engine_power)
        year = try? values.decode(Int.self, forKey: .year)
        mileage = try? values.decode(Int.self, forKey: .mileage)
        
        
        id = try? values.decode(Int.self, forKey: .id)
        carName = try? values.decode(String.self, forKey: .title)
        
        if let _dtp_status = try? values.decode(HistoryStatus.self, forKey: .dtp_status) {
            dtp_status = _dtp_status
        }
        
        if let _history_status = try? values.decode(HistoryStatus.self, forKey: .history_status) {
            history_status = _history_status
        }
        
        if let state_number = try? values.decode(String.self, forKey: .state_number), let vin = try? values.decode(String.self, forKey: .vin) {
            carNumber = convertCarNumber(number: state_number, vin: vin)
        }
    }
    
    
    
    init(id: Int, carNumber: CarNumber?,
         carName:String? = nil,
         color: CarColor? = nil,
         marka: Marka? = nil,
         model:Model? = nil,
         generation:Generation? = nil,
         carType: IdAndName?=nil,
         serie: Serie? = nil,
         modification: Modification? = nil, mileage: Int? = nil) {
            
            self.id = id
            self.carNumber = carNumber
            self.carName = carName
            self.color = color
            self.marka = marka
            self.model = model
            self.generation = generation
            self.serie = serie
            self.modification = modification
            self.carType = carType
        self.mileage = mileage
        self.history_status = HistoryStatus(is_already_purchased: false, count: 0, order_id: 0)
        self.dtp_status = HistoryStatus(is_already_purchased: false, count: 0, order_id: 0)
    }
    
    
    
    mutating func convertCarNumber (number: String, vin: String) -> CarNumber {
        let _number = number
        var _carNumber = CarNumber(isNewFormat: true, number: 0, letters: "", cityId: "", vin: vin)
        
        if number.count == 8 {
            let firstIndex = _number.index(_number.startIndex, offsetBy: 3)
            let secondIndex = _number.index(_number.startIndex, offsetBy: 6)

            let numbers = _number[..<firstIndex]
            let letters = _number[firstIndex..<secondIndex]
            let cityId = _number[secondIndex..<_number.endIndex]
            _carNumber.number = Int(numbers)!
            _carNumber.letters = String(letters)
            _carNumber.cityId = String(cityId)
            _carNumber.isNewFormat = true
        } else {
            let firstIndex = _number.index(_number.startIndex, offsetBy: 1)
            let secondIndex = _number.index(_number.startIndex, offsetBy: 4)
            
            let cityId = _number[..<firstIndex]
            let numbers = _number[firstIndex..<secondIndex]
            let letters = _number[secondIndex..<_number.endIndex]
            _carNumber.number = Int(numbers)!
            _carNumber.letters = String(letters)
            _carNumber.cityId = String(cityId)
            _carNumber.isNewFormat = false
        }
        
        return _carNumber
    }
}
